<?php
namespace App\Exceptions;

use Exception;

class BaseException extends Exception
{
    const HTTP_OK = 200;

    protected $data;

    protected $code;

    public function __construct($data, int $code = self::HTTP_OK, array $meta = [])
    {
        $this->data = $data;
        $this->code = $code;
        $this->meta = $meta;
        // parent::__construct($data, $code);
    }

    public function render()
    {
        $httpStatus = getHttpStatus();

        $content = [];
        if (is_array($this->data)) {
            $content = $this->data;
        }

        if (is_string($this->data)) {
            $content = in_array($this->code, $httpStatus)
                ? [
                    'message' => $this->data
                ]
                : [
                    'message' => $this->data,
                    'code'    => $this->code,
                    //                    'timestamp' => time()
                ];
        }

        if ($this->meta) {
            $content = array_add($content, 'meta', $this->meta);
        }

        // 不加baseRequests
        return response()->json($content, $this->code);
    }
}
